package com.shundr.truck.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shundr.R;
import com.shundr.common.util.LImageLoader;
import com.shundr.frame.util.StringUtil;
import com.shundr.truck.model.TruckInfo;

/**
 * 我的车辆列表适配器
 * TruckAdapter
 * 
 * @author larry 2015-4-27 下午4:30:10 
 * @version 1.0.0
 *
 */
public class TruckAdapter extends BaseAdapter {
	private List<TruckInfo> mList;
	private Context mContext;
	private ImageLoader mImageLoader;

	public TruckAdapter(Context context, List<TruckInfo> list) {
		this.mContext = context;
		this.mList = list;
		this.mImageLoader = ImageLoader.getInstance();
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHold viewHold = null;
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_truck_info,null);
			viewHold = new ViewHold();
			viewHold.TruckCount = (TextView) convertView.findViewById(R.id.tv_type);
			viewHold.TruckNum = (TextView) convertView.findViewById(R.id.tv_code);
			viewHold.TruckPicture = (ImageView) convertView.findViewById(R.id.iv_item_truck_picture);
			viewHold.tvStatus = (TextView) convertView.findViewById(R.id.iv_1);
			viewHold.TruckValidation = (ImageView)convertView.findViewById(R.id.iv_validation);
			convertView.setTag(viewHold);
		}else{
			viewHold = (ViewHold) convertView.getTag();
		}
		try {
			TruckInfo info = mList.get(position);
			if (!StringUtil.isNull(info.getTruckPlateNumber())) {
				viewHold.TruckNum.setText(info.getTruckPlateNumber());
			}
			if(info.getTruckStatus() == 3){
				viewHold.tvStatus.setText(mContext.getResources().getString(R.string.truck_freeing));
				viewHold.tvStatus.setTextColor(mContext.getResources().getColor(R.color.btn_green_light));
			}else{
				viewHold.tvStatus.setText(mContext.getResources().getString(R.string.truck_busying));
				viewHold.tvStatus.setTextColor(mContext.getResources().getColor(R.color.btn_orange_light));
			}
			if(info.getTruckValidationStatus() == 0){//待审核
				viewHold.TruckValidation.setVisibility(View.VISIBLE);
				viewHold.TruckValidation.setImageResource(R.drawable.iv_truck_validating);
			}else if(info.getTruckValidationStatus() == 1){//审核通过
				viewHold.TruckValidation.setImageResource(R.drawable.iv_truck_validation_successed);
				viewHold.TruckValidation.setVisibility(View.VISIBLE);
			}else if(info.getTruckValidationStatus() == 2){//未审核通过
				viewHold.TruckValidation.setImageResource(R.drawable.iv_truck_validation_failed);
				viewHold.TruckValidation.setVisibility(View.VISIBLE);
			}else{
				viewHold.TruckValidation.setVisibility(View.GONE);
			}
			String type = info.getTruckType() == null ? "" : (info.getTruckType() + " ");
			if(StringUtil.isEqual(info.getTruckLength(), "23")){
				viewHold.TruckCount.setText(type+info.getTruckCapacity() + " 吨 " + info.getTruckLength() + "米以上" );
			}else{
				viewHold.TruckCount.setText(type+info.getTruckCapacity() + " 吨 " + info.getTruckLength() + "米" );
			}
//		    String url = "http://h.hiphotos.baidu.com/image/pic/item/377adab44aed2e73146edc2d8501a18b87d6fa28.jpg";
			mImageLoader.displayImage(info.getTruckPic(), viewHold.TruckPicture,LImageLoader.mOptions);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	class ViewHold {
		/**
		 * 车的序号
		 */
		TextView TruckCount;
		/**
		 * 车牌号码
		 */
		TextView TruckNum;
		/**
		 * 车辆照片
		 */
		ImageView TruckPicture;
		TextView tvStatus;
		ImageView TruckValidation;
	}
}
